package cn.hxzy.service.impl;

import cn.hxzy.common.R;
import cn.hxzy.entity.Orders;
import cn.hxzy.entity.Train;
import cn.hxzy.entity.User;
import cn.hxzy.mapper.OrdersMapper;
import cn.hxzy.mapper.UserMapper;
import cn.hxzy.service.UserService;
import cn.hxzy.util.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public R register(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",user.getUserName());
        User one = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(one)){
            int insert = userMapper.insert(user);
            if (insert>0){
                redisTemplate.opsForList().leftPush("user",user);
                return R.ok().code(1).message("注册成功");
            }else {
                return R.error().code(-1).message("注册失败");
            }
        }else {
            return R.error().code(-2).message("注册失败");
        }
    }

    @Override
    public R login(String userName, String userPwd) {
        if (StringUtils.isEmpty(userName)||StringUtils.isEmpty(userPwd)) {
            return R.error().code(-1).message("账号或密码为空");
        }else {
            String jwt = null;
            List<User> users = redisTemplate.opsForList().range("user", 0, -1);
            for (User user:users){
                if (user.getUserName().equals(userName)&&user.getUserPwd().equals(userPwd)){
                    jwt = JWTUtil.createJwt(userName);
                    redisTemplate.opsForValue().set(userName,jwt,1800L, TimeUnit.SECONDS);
                    return R.ok().code(20000).message("登录成功").authorization(jwt);
                }
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_name",userName);
            queryWrapper.eq("status",0);
            User user1 = userMapper.selectOne(queryWrapper);
            if (Objects.isNull(user1)){
                return R.error().code(-2).data("item","用户未注册");
            }else {
                if (user1.getUserPwd().equals(userPwd)){
                    redisTemplate.opsForList().leftPush("user",user1);
                    jwt = JWTUtil.createJwt(userName);
                    redisTemplate.opsForValue().set(userName,jwt,1800L, TimeUnit.SECONDS);
                    return R.ok().code(20000).message("登录成功").authorization(jwt);
                }else {
                    return R.error().code(-20000).message("密码错误").data("item", null);
                }
            }
        }
    }

    @Override
    public R lookOrder(String userName) {
        if (StringUtils.isEmpty(userName)){
            return R.error().message("请先登录").data("item",null);
        }
        List<Orders> myOrders = redisTemplate.opsForList().range("myOrders"+userName, 0, -1);
        if (myOrders.size()<1){
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("user_name",userName);
            User user = userMapper.selectOne(queryWrapper1);
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("user_id",user.getUserId());
            List<Orders> list = ordersMapper.selectList(queryWrapper2);
            if (list.size()>0){
                for (Orders orders:list){
                    Train t = (Train) redisTemplate.opsForValue().get(orders.getTrainId().toString());
                    orders.setTrainName(t.getTrainName());
                    orders.setUserName(userName);
                    redisTemplate.opsForList().leftPush("myOrders"+userName,orders);
                }
                return R.ok().message("订单查询成功").data("item",list);
            }else {
                return R.error().message("暂无订单").data("item",null);
            }
        }else {
            for (Orders orders:myOrders){
                Train t = (Train) redisTemplate.opsForValue().get(orders.getTrainId().toString());
                orders.setTrainName(t.getTrainName());
                orders.setUserName(userName);
            }
            return R.ok().message("订单查询成功").data("item",myOrders);
        }
    }


}
